﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SQLite;
using System.IO;

namespace Logistics.DataManage.DB
{
    class ConnectStringMgr
    {
        private static String m_ConnString_Sqlite;
        public static string ConnectString_Sqlite
        {
            get
            {
                if (m_ConnString_Sqlite == null)
                {
                    ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["SQLITE"];
                    if (setting == null || String.IsNullOrEmpty(setting.ConnectionString))
                    {
                        m_ConnString_Sqlite = GetDefaultSQLiteConnectionString();
                        if (String.IsNullOrEmpty(m_ConnString_Sqlite))
                            throw new ConfigurationErrorsException("请在App.config文件中配置本地SQLITE数据库连接串");
                    }
                    else
                        m_ConnString_Sqlite = setting.ConnectionString;
                    if (m_ConnString_Sqlite.Contains("|APP_ROOT|"))
                    {
                        String appPath = AppDomain.CurrentDomain.BaseDirectory;
                        appPath = appPath.Replace("\\", "/");
                        m_ConnString_Sqlite = m_ConnString_Sqlite.Replace("|APP_ROOT|", appPath);
                    }
                }
                return m_ConnString_Sqlite;
            }
        }

        private static String m_SqliteDbLocation;
        public static string SqliteDbLocation
        {
            get
            {
                if (m_SqliteDbLocation == null)
                {
                    //Data Source=D:\PProj\SRC\sqlite.db;Version=3;Password=myPassword;Pooling=True;Max Pool Size=100"
                    String filePath = GetSQLiteFileLocation(ConnectString_Sqlite);
                    if (!String.IsNullOrWhiteSpace(filePath))
                        m_SqliteDbLocation = filePath;
                }
                return m_SqliteDbLocation;
            }
        }

        private static String m_ConnectString_Oracle;
        public static string ConnectString_Oracle
        {
            get
            {
                if (m_ConnectString_Oracle == null)
                {
                    ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["ORACLE"];
                    if (setting == null || String.IsNullOrEmpty(setting.ConnectionString))
                        throw new ConfigurationErrorsException("请在App.config文件中配置本地ORACLE数据库连接串");
                    m_ConnectString_Oracle = setting.ConnectionString;
                }
                return m_ConnectString_Oracle;
            }
            set
            {
                m_ConnectString_Oracle = value;
            }
        }

        private static String GetSQLiteFileLocation(String connectionString)
        {
            if (!String.IsNullOrWhiteSpace(connectionString))
            {
                String[] segments = connectionString.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries);
                foreach (var seg in segments)
                {
                    String[] keyValues = seg.Split(new char[]{'='},StringSplitOptions.RemoveEmptyEntries);
                    if (keyValues.Length > 1)
                    {
                        if(String.Compare("Data Source",keyValues[0].Trim(),true)==0)
                            return keyValues[1];
                    }
                }
            }
            return "";
        }

        private static String GetDefaultSQLiteConnectionString()
        {
            String relative = "App_Data/logistics.db";
            String absPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relative);
            if (File.Exists(absPath))
            {
                String connectString = String.Format("Data Source={0};Version=3;Pooling=True;Max Pool Size=100", absPath);
                return connectString;
            }
            return "";
        }
    }
}
